package com.tansun.easycare.rule.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.rule.common.model.BaseBean;
import com.tansun.easycare.rule.common.model.Bom;
import com.tansun.easycare.rule.common.model.BomField;
import com.tansun.easycare.rule.common.model.Domain;
import com.tansun.easycare.rule.common.model.DomainItem;
import com.tansun.easycare.rule.common.model.ListTail;

/**
 * 编写规则的 元数据;
 *
 * @author WY
 */
public class BomCreator {


    /**
     * bom
     */
    public static String initTopBom(Map<String, Bom> map) {
        return JSON.toJSONString(findTopObject(map));
    }

    /**
     * 所有顶层对象 删除对象的field列表 减小json 对象
     */
    public static List<Bom> findInOut(Map<String, Bom> metaMap) {
        if (null == metaMap) {
            return null;
        }
        List<Bom> list = new ArrayList<Bom>();
        Set<String> setStr = metaMap.keySet();
        for (String ketStr : setStr) {
            Bom mclass = metaMap.get(ketStr);
            Bom copied = copyBom(mclass);
            String paramType = mclass.getParamType();
            if (null == paramType || "".equals(paramType.trim())) {
                continue;
            }
            if ("IN".equals(paramType.trim()) || "OUT".equalsIgnoreCase(paramType.trim()) || "All".equalsIgnoreCase(paramType.trim())||"funcparam".equalsIgnoreCase(paramType.trim())) {
                list.add(copied);
            }

        }
        return list;
    }


    /**
     * 所有顶层对象 删除对象的field列表 减小json 对象
     */
    public static Map<String, Bom> findMapInOut(Map<String, Bom> metaMap) {
        if (null == metaMap) {
            return null;
        }
        Map<String, Bom> map = new HashMap();
        Set<String> setStr = metaMap.keySet();
        for (String ketStr : setStr) {
            Bom mclass = metaMap.get(ketStr);
            Bom copied = copyBom(mclass);
            String paramType = mclass.getParamType();
            if (null == paramType || "".equals(paramType.trim())) {
                continue;
            }
            if ("IN".equals(paramType.trim()) || "OUT".equalsIgnoreCase(paramType.trim()) || "All".equalsIgnoreCase(paramType.trim())) {
                map.put(ketStr, copied);
            }

        }
        return map;
    }

    public static Bom copyBom(Bom bom) {
        Bom result = new Bom();
        result.setDesc(bom.getDesc());
        result.setId(bom.getId());
        result.setLabel(bom.getLabel());
        result.setName(bom.getName());
        result.setParamType(bom.getParamType());
        result.setType(bom.getType());
        result.setDataType(bom.getDataType());
        return result;
    }

    /**
     * 获取所有顶层对象
     */
    public static List<Bom> findTopObject(Map<String, Bom> metaMap) {
        if (null == metaMap) {
            return null;
        }
        List<Bom> list = new ArrayList<Bom>();
        Set<String> setStr = metaMap.keySet();
        for (String ketStr : setStr) {
            Bom mclass = metaMap.get(ketStr);

            String paramType = mclass.getParamType();
            if (null == paramType || "".equals(paramType.trim())) {
                continue;
            }
            if ("IN".equals(paramType.trim()) || "OUT".equalsIgnoreCase(paramType.trim()) || "All".equalsIgnoreCase(paramType.trim())) {
                list.add(mclass);
            }

        }
        return list;
    }

    /**
     * 获取所有顶层对象
     */
    public static Map<String, Bom> findTopMap(Map<String, Bom> metaMap) {
        if (null == metaMap) {
            return null;
        }
        Map<String, Bom> map = new HashMap<String, Bom>();

        Set<String> setStr = metaMap.keySet();
        for (String ketStr : setStr) {
            Bom mclass = metaMap.get(ketStr);
            String paramType = mclass.getParamType();
            if (null == paramType || "".equals(paramType.trim())) {
                continue;
            }
            if ("IN".equals(paramType.trim()) || "OUT".equalsIgnoreCase(paramType.trim()) || "All".equalsIgnoreCase(paramType.trim())) {
                map.put(ketStr, metaMap.get(ketStr));
            }
        }
        return map;
    }

    public static Map<String, Bom> buildMap(Map<String, Bom> metaMap) {
        if (null == metaMap) {
            return null;
        }
        Map<String, Bom> insertMap = new HashMap<String, Bom>();
        Set<String> setStr = metaMap.keySet();
        for (String ketStr : setStr) {
            Bom mclass = metaMap.get(ketStr);
            //Map objMap = new HashMap<String, Object>();
            String paramType = mclass.getParamType();
            if (null == paramType || "".equals(paramType.trim())) {
                continue;
            }
            if ("IN".equals(paramType.trim()) || "OUT".equalsIgnoreCase(paramType.trim()) || "All".equalsIgnoreCase(paramType.trim())) {
                Bom bom = buildMapByMeteClass(mclass, metaMap);
                insertMap.put(mclass.getName(), bom);
            }

        }
        return insertMap;
    }

    private static Bom buildMapByMeteClass(Bom mclass, Map<String, Bom> metaMap) {

        Bom objMap = new Bom();

        List<BomField> listField = mclass.getBomFields();

        for (Iterator<BomField> iterator = listField.iterator(); iterator.hasNext(); ) {
            BomField mfield = iterator.next();


            if (objMap.getBomFields() == null) {
                objMap.setBomFields(new ArrayList<BomField>());
            }

        }
        return objMap;
    }


    /**
     * 实例化列表尾部
     */
    public static Map<String, ListTail> getListTailMap() {
        ListTail lt1 = new ListTail();
        lt1.setId("uei129fd");
        lt1.setLabel("长度");
        lt1.setName("size");
        lt1.setType(BaseBean.TYPE_LISTTAIL);
        lt1.setDataType("Long");

        ListTail lt2 = new ListTail();
        lt2.setId("uei189fd");
        lt2.setLabel("项");
        lt2.setName("item");
        lt2.setType(BaseBean.TYPE_LISTTAIL);


        Map<String, ListTail> map = new HashMap<String, ListTail>();
        map.put("size", lt1);
        map.put("item", lt2);
        return map;

    }

    public static DomainItem getDomainItemById(String id, Map<String, Domain> domains) {
        Set<String> keys = domains.keySet();

        for (String key : keys) {
            List<DomainItem> list = domains.get(key).getDomainItems();
            for (DomainItem domainItem : list) {
                if (domainItem.getId().equals(id)) {
                    return domainItem;
                }
            }

        }
        return null;
    }

}
